package atomic;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * 描述：演示AtomicInteger的基本用法,对比非原子类的线程安全问题
 */

public class AtomicIntegerDemo1 {
    private static volatile int count = 0;
    private static void increase() {
        count++;
    }
    private static AtomicInteger atomicCount = new AtomicInteger(0);
    private static void atomicIncrease() {
        atomicCount.incrementAndGet();
    }

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 10000; i++) {
                increase();
                atomicIncrease();
            }
        }, "t1");
        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 10000; i++) {
                increase();
                atomicIncrease();
            }
        }, "t2");
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println("count = " + count);// count = 15816
        System.out.println("atomicCount = " + atomicCount.get());// atomicCount = 20000
    }
}
